package gui;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import database.DBHelper;

import user.User;




/**
 * Login Frame
 * @author warrenshenk
 */
public class LoginFrame extends JFrame {

///////////////////////////////////////////////////////////////////////////////////////
// INSTANCE VARIABLES
///////////////////////////////////////////////////////////////////////////////////////

	private static final long serialVersionUID = 1L;
	private JPanel contentPane, panel_0, panel_1, panel_2, panel_3;
	private JTextField userField, passwordField;
	private JButton registerBttn, loginBttn;
	private JLabel userLabel, passwordLabel;

///////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
///////////////////////////////////////////////////////////////////////////////////////

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setTitle("GT PORT");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(6, 1, 0, 0));
		
		panel_0 = new JPanel();
		contentPane.add(panel_0);
		
		userLabel = new JLabel("Username: ");
		panel_0.add(userLabel);
		
		userField = new JTextField();
		panel_0.add(userField);
		userField.setColumns(10);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1);
		
		passwordLabel = new JLabel("Password: ");
		panel_1.add(passwordLabel);
		
		passwordField = new JPasswordField();
		panel_1.add(passwordField);
		passwordField.setColumns(10);
		
		panel_2 = new JPanel();
		contentPane.add(panel_2);
		
		loginBttn = new JButton("Login");
		panel_2.add(loginBttn);
		loginBttn.addActionListener(loginListener);
		
		panel_3 = new JPanel();
		contentPane.add(panel_3);
		
		registerBttn = new JButton("Register New Account");
		panel_3.add(registerBttn);
		registerBttn.addActionListener(registerListener);
	}
	//-----------------------------------------------------------------------------------
	

	
///////////////////////////////////////////////////////////////////////////////////////
// BUTTON LISTENERS
///////////////////////////////////////////////////////////////////////////////////////
	/**
	 * Button Listener for the Login Button
	 */
	private ActionListener loginListener = new ActionListener() { 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			DBHelper db = new DBHelper();
			db.open();
			
			if (db.isLoginValid(userField.getText(), passwordField.getText())) {
				
				User user = new User(userField.getText());
				
				user.dumpData(); //prints data to console
				
				HomePageFrame hFrame = new HomePageFrame(user);
				hFrame.setVisible(true);
				LoginFrame.this.dispose();
			}
			else 
				new JOptionPane().showMessageDialog(LoginFrame.this, "Invalid Username or Password");
			
			db.close();
		}
	};
	//-----------------------------------------------------------------------------------
	
	/**
	 * Button Listener for the Register Button
	 */
	private ActionListener registerListener = new ActionListener() { 
		@Override
		public void actionPerformed(ActionEvent arg0) {
			RegisterUserFrame regFrame = new RegisterUserFrame();
			regFrame.setVisible(true);
			LoginFrame.this.dispose();
		}
	};
	//-----------------------------------------------------------------------------------

	
	

}
